﻿using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;

namespace cmdb.keede.entity.Migrations
{
    public partial class Initalize : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "EnvironmentInfo",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Description = table.Column<string>(unicode: false, maxLength: 500, nullable: true),
                    Name = table.Column<string>(unicode: false, maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_EnvironmentInfo", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "User",
                columns: table => new
                {
                    Id = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    email = table.Column<string>(unicode: false, maxLength: 50, nullable: true),
                    Name = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    Password = table.Column<string>(unicode: false, maxLength: 128, nullable: false),
                    Role = table.Column<string>(unicode: false, maxLength: 10, nullable: false),
                    Status = table.Column<string>(unicode: false, maxLength: 10, nullable: false),
                    Tel = table.Column<string>(unicode: false, maxLength: 20, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_User", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "BusinessInfo",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: false),
                    Department = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    Description = table.Column<string>(unicode: false, maxLength: 500, nullable: true),
                    EnvironmentId = table.Column<int>(nullable: true),
                    Name = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    PIC = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    Type = table.Column<string>(unicode: false, maxLength: 10, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_BusinessInfo", x => x.Id);
                    table.ForeignKey(
                        name: "FK_BUSINESS_REFERENCE_ENVIRONM",
                        column: x => x.EnvironmentId,
                        principalTable: "EnvironmentInfo",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_BUSINESS_REFERENCE_USER",
                        column: x => x.PIC,
                        principalTable: "User",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "Group",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: false),
                    Creator = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    Description = table.Column<string>(unicode: false, maxLength: 500, nullable: true),
                    Name = table.Column<string>(unicode: false, maxLength: 1, nullable: false),
                    Status = table.Column<string>(unicode: false, maxLength: 10, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Group", x => x.Id);
                    table.ForeignKey(
                        name: "FK_GROUP_REFERENCE_USER",
                        column: x => x.Creator,
                        principalTable: "User",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "OperationLog",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    ExecTime = table.Column<TimeSpan>(nullable: false),
                    OperationContent = table.Column<string>(unicode: false, maxLength: 500, nullable: false),
                    OperationTime = table.Column<DateTime>(type: "datetime", nullable: false),
                    Operator = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    Result = table.Column<string>(unicode: false, maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OperationLog", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OPERATIO_REFERENCE_USER",
                        column: x => x.Operator,
                        principalTable: "User",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "VirtualHostInfo",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Area = table.Column<string>(unicode: false, maxLength: 255, nullable: false, defaultValueSql: "('')"),
                    AssetId = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    CPU = table.Column<int>(nullable: false, defaultValueSql: "((0))"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: false),
                    DeviceType = table.Column<string>(unicode: false, maxLength: 10, nullable: false),
                    ExpireTime = table.Column<DateTime>(type: "datetime", nullable: false, defaultValueSql: "('2099-1-1')"),
                    HardDiskSize = table.Column<decimal>(type: "decimal(1, 0)", nullable: false, defaultValueSql: "((0))"),
                    InnerIP = table.Column<int>(nullable: false),
                    Maintainers = table.Column<string>(unicode: false, maxLength: 50, nullable: true),
                    NetworkBandwidth = table.Column<int>(nullable: false, defaultValueSql: "((1000))"),
                    OS = table.Column<string>(unicode: false, maxLength: 32, nullable: false),
                    OuterIP = table.Column<int>(nullable: false),
                    Password = table.Column<string>(unicode: false, maxLength: 256, nullable: true),
                    RAM = table.Column<decimal>(type: "decimal(1, 0)", nullable: false, defaultValueSql: "((0))"),
                    Remark = table.Column<string>(unicode: false, maxLength: 255, nullable: true),
                    Status = table.Column<string>(unicode: false, maxLength: 10, nullable: false),
                    UserName = table.Column<string>(unicode: false, maxLength: 50, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_VirtualHostInfo", x => x.Id);
                    table.ForeignKey(
                        name: "FK_VIRTUALH_REFERENCE_USER",
                        column: x => x.Maintainers,
                        principalTable: "User",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "ApplicationInfo",
                columns: table => new
                {
                    Name = table.Column<string>(unicode: false, maxLength: 50, nullable: false),
                    BusinessId = table.Column<int>(nullable: true),
                    Description = table.Column<string>(unicode: false, maxLength: 500, nullable: true),
                    PIC = table.Column<string>(unicode: false, maxLength: 50, nullable: true),
                    Port = table.Column<string>(unicode: false, maxLength: 10, nullable: false),
                    Type = table.Column<string>(unicode: false, maxLength: 20, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ApplicationInfo", x => x.Name);
                    table.ForeignKey(
                        name: "FK_APPLICAT_REFERENCE_BUSINESS",
                        column: x => x.BusinessId,
                        principalTable: "BusinessInfo",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_APPLICAT_REFERENCE_USER",
                        column: x => x.PIC,
                        principalTable: "User",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "GroupHost",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    GroupId = table.Column<int>(nullable: false),
                    HostId = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GroupHost", x => x.Id);
                    table.ForeignKey(
                        name: "FK_GROUP_HO_REFERENCE_GROUP",
                        column: x => x.GroupId,
                        principalTable: "Group",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_GROUP_HO_REFERENCE_VIRTUALH",
                        column: x => x.HostId,
                        principalTable: "VirtualHostInfo",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "ApplicationHost",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    ApplicationName = table.Column<string>(unicode: false, maxLength: 50, nullable: true),
                    HostId = table.Column<int>(nullable: true),
                    Name = table.Column<string>(unicode: false, maxLength: 50, nullable: true),
                    Operator = table.Column<string>(unicode: false, maxLength: 50, nullable: true),
                    Status = table.Column<string>(unicode: false, maxLength: 10, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ApplicationHost", x => x.Id);
                    table.ForeignKey(
                        name: "FK_APPLICAT_REFERENCE_APPLICAT",
                        column: x => x.ApplicationName,
                        principalTable: "ApplicationInfo",
                        principalColumn: "Name",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_APPLICAT_REFERENCE_VIRTUALH",
                        column: x => x.HostId,
                        principalTable: "VirtualHostInfo",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_ApplicationHost_ApplicationName",
                table: "ApplicationHost",
                column: "ApplicationName");

            migrationBuilder.CreateIndex(
                name: "IX_ApplicationHost_HostId",
                table: "ApplicationHost",
                column: "HostId");

            migrationBuilder.CreateIndex(
                name: "IX_ApplicationInfo_BusinessId",
                table: "ApplicationInfo",
                column: "BusinessId");

            migrationBuilder.CreateIndex(
                name: "IX_ApplicationInfo_PIC",
                table: "ApplicationInfo",
                column: "PIC");

            migrationBuilder.CreateIndex(
                name: "IX_BusinessInfo_EnvironmentId",
                table: "BusinessInfo",
                column: "EnvironmentId");

            migrationBuilder.CreateIndex(
                name: "IX_BusinessInfo_PIC",
                table: "BusinessInfo",
                column: "PIC");

            migrationBuilder.CreateIndex(
                name: "IX_Group_Creator",
                table: "Group",
                column: "Creator");

            migrationBuilder.CreateIndex(
                name: "IX_GroupHost_GroupId",
                table: "GroupHost",
                column: "GroupId");

            migrationBuilder.CreateIndex(
                name: "IX_GroupHost_HostId",
                table: "GroupHost",
                column: "HostId");

            migrationBuilder.CreateIndex(
                name: "IX_OperationLog_Operator",
                table: "OperationLog",
                column: "Operator");

            migrationBuilder.CreateIndex(
                name: "IX_VirtualHostInfo_Maintainers",
                table: "VirtualHostInfo",
                column: "Maintainers");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "ApplicationHost");

            migrationBuilder.DropTable(
                name: "GroupHost");

            migrationBuilder.DropTable(
                name: "OperationLog");

            migrationBuilder.DropTable(
                name: "ApplicationInfo");

            migrationBuilder.DropTable(
                name: "Group");

            migrationBuilder.DropTable(
                name: "VirtualHostInfo");

            migrationBuilder.DropTable(
                name: "BusinessInfo");

            migrationBuilder.DropTable(
                name: "EnvironmentInfo");

            migrationBuilder.DropTable(
                name: "User");
        }
    }
}
